*	************************************************************************
* 	File-Name: 		.do
*	Date:  			04/26/2016
*	Authors: 		Eugenie Dugoua
*	Data Used:  		Data_Merged_7_05.dta
*	Purpose:   		Analysis
*	Software:		Stata SE 12.1
*	Machine:		Stata for Windows through Linux Wine
*	************************************************************************

*	************************************************************************
*	DIRECTORY                                                               
*	************************************************************************
cd "/home/emd/Dropbox/Night Lights Validation (Eugenie, Ryan, Johannes)"

*JU FILE PATH, WD
*cd "/home/jurpelai/Dropbox/Night Lights Validation (Eugenie, Ryan, Johannes)"

*RK File Path, WD
*cd "C:/Users/Ryan/Dropbox/Night Lights Validation (Eugenie, Ryan, Johannes)"

clear all
use "./Data/Hours/hours_lights_CLEAN.dta"
duplicates drop
duplicates tag c11_2011_vill_code, gen(ID_Duplicates)
tab ID_Duplicates
sort c11_2011_vill_code
drop if ID_Duplicates == 1
drop ID_Duplicates
save "./Data/Hours/hours_lights_CLEAN.dta", replace

clear all
use "./Data/Python_code_merging/Data_Merged_11_20_stata.dta"
drop summer_hours winter_hours elec_hours log_sum_hrs

mmerge Vill11 using "./Data/Hours/hours_lights_CLEAN.dta", type(n:n) umatch(c11_2011_vill_code)
* matched 640923 villages. 8575 not matched
drop if _merge == 2
drop _merge
gen elec_hours = (summer_hours + winter_hours)/2

gen log_sum_hrs = log(elec_hours*electrified_hh_nbr+1)
label variable log_sum_hrs "Hours of Electricity * Electrified HH (log)"

save "./Data/Python_code_merging/Data_Merged_11_20_stata.dta", replace


clear all
use "./Data/Python_code_merging/Data_Merged_11_20_stata.dta"

**************************************
* REGRESSION 1: MAIN             
**************************************
label variable elec_hours "Hours per day of electricity"

gen light_hours_inte_dumm = SH_sum_11_logdummy * elec_hours
label variable light_hours_inte_dumm "No Night lights dummy X Hours per day of electricity"

gen light_hours_inte = SH_sum_11_log * elec_hours
label variable light_hours_inte "Night lights dummy X Hours per day of electricity"

gen hours5 = 0 
replace hours5 = 1 if elec_hours < 5
gen hours10 = 0 
replace hours10 = 1 if elec_hours >= 5 & elec_hours < 10
gen hours15 = 0 
replace hours15 = 1 if elec_hours >= 10 & elec_hours < 15
gen hours20 = 0 
replace hours20 = 1 if elec_hours >= 15 & elec_hours < 20

gen nightXhours5 = SH_sum_11_log * hours5
gen nightXhours10 = SH_sum_11_log * hours10
gen nightXhours15 = SH_sum_11_log * hours15
gen nightXhours20 = SH_sum_11_log * hours20
label variable nightXhours5 "Night lights X If elec < 5 h"
label variable nightXhours10 "Night lights X If elec between 5 and 10 h"
label variable nightXhours15 "Night lights X If elec between 10 and 15 h"
label variable nightXhours20 "Night lights X If elec between 15 and 20 h"

gen nightdummyXhours5 = SH_sum_11_logdummy * hours5
gen nightdummyXhours10 = SH_sum_11_logdummy * hours10
gen nightdummyXhours15 = SH_sum_11_logdummy * hours15
gen nightdummyXhours20 = SH_sum_11_logdummy * hours20
label variable nightdummyXhours5 "Night lights dummy X If elec < 5 h"
label variable nightdummyXhours10 "Night lights dummy X If elec between 5 and 10 h"
label variable nightdummyXhours15 "Night lights dummy X If elec between 10 and 15 h"
label variable nightdummyXhours20 "Night lights dummy X If elec between 15 and 20 h"

***************************************************************************************
eststo clear
eststo: quietly reg electrified_hh_nbr_log SH_sum_11_log SH_sum_11_logdummy, vce(cluster St11)
eststo: quietly reg electrified_hh_nbr_log SH_sum_11_log SH_sum_11_logdummy if elec_hours < 5, vce(cluster St11)
eststo: quietly reg electrified_hh_nbr_log SH_sum_11_log SH_sum_11_logdummy if elec_hours >= 5 & elec_hours < 10, vce(cluster St11)
eststo: quietly reg electrified_hh_nbr_log SH_sum_11_log SH_sum_11_logdummy if elec_hours >= 10 & elec_hours < 15, vce(cluster St11)
eststo: quietly reg electrified_hh_nbr_log SH_sum_11_log SH_sum_11_logdummy if elec_hours >= 15 & elec_hours < 20, vce(cluster St11)

esttab using "./Manuscript/Tables/results_elechours1.tex", ///
replace b(%9.3f)  ///
stats(r2 N, fmt(%9.3f %9.3g) label("R-squared" "Observations")) booktabs eqlabels(none) ///
noconstant se label star(* 0.10 ** 0.05 *** 0.01)  ///
addnote ("Dependent variable: Number of households with electricity (log).") ///
mtitles("All" "< 5 h" "Btw 5 and 10 h" "Btw 10 and 15 h" "Btw 15 and 20 h")


***************************************************************************************
eststo clear
eststo: quietly reg electrified_hh_nbr_log     SH_sum_11_log elec_hours if SH_sum_11_log>0, vce(cluster St11)
eststo: quietly reg electrified_hh_nbr_log     SH_sum_11_log nightXhours5 if SH_sum_11_log>0, vce(cluster St11)
eststo: quietly reg electrified_hh_nbr_log     SH_sum_11_log nightXhours10 if SH_sum_11_log>0, vce(cluster St11)
eststo: quietly reg electrified_hh_nbr_log     SH_sum_11_log nightXhours15 if SH_sum_11_log>0, vce(cluster St11)
eststo: quietly reg electrified_hh_nbr_log     SH_sum_11_log nightXhours20 if SH_sum_11_log>0, vce(cluster St11)

esttab using "./Manuscript/Tables/results_elechours2.tex", ///
replace b(%9.3f)  ///
stats(r2 N, fmt(%9.3f %9.3g) label( "R-squared" "Observations")) booktabs eqlabels(none) ///
noconstant se label star(* 0.10 ** 0.05 *** 0.01) nomtitles ///
addnote ("Dependent variable: Number of households with electricity (log)." ///
"Sub0sample of villages with night lights striclty positive")

******************************************************
gen hhxhours = electrified_hh_nbr_log * elec_hours
label variable hhxhours "Electrified HH (log nbr) X Hours per day of elec."

eststo clear
eststo: quietly reg SH_sum_11_log    electrified_hh_nbr_log, vce(cluster St11)
eststo: quietly reg SH_sum_11_log    elec_hours, vce(cluster St11)
eststo: quietly reg SH_sum_11_log    electrified_hh_nbr_log elec_hours, vce(cluster St11)
eststo: quietly reg SH_sum_11_log    electrified_hh_nbr_log elec_hours hhxhours, vce(cluster St11)
eststo: quietly reg SH_sum_11_log    electrified_hh_nbr_log hhxhours, vce(cluster St11)
eststo: quietly reg SH_sum_11_log    hhxhours, vce(cluster St11)

esttab using "./Manuscript/Tables/results_elechours3.tex", ///
replace b(%9.3f)  ///
stats(r2 N, fmt(%9.3f %9.3g) label("R-squared" "Observations")) booktabs eqlabels(none) ///
noconstant se label star(* 0.10 ** 0.05 *** 0.01) nomtitles ///
addnote ("Dependent variable: Night lights (shape, log sum, 2011).")



corr electrified_hh_nbr_log elec_hours











